package example.customscan;

import java.io.File;

public class CustomScamUtils {
	public void scan(String basePackage) {
		//由于部署到生产环境后，源码已经不存在，所以需要获取编译后的classpath路径
		String rootPath = this.getClass().getResource("/").getPath();
		String basePacakgePath = basePackage.replace(".", "\\");
		File file = new File(rootPath + "\\" + basePacakgePath);
		//	遍历file
		String[] list = file.list();
		for (String name :
				list) {
			name = name.replaceAll(".class", "");
			try {
				Class clazz =  Class.forName(basePackage+"."+name);
				//判断是否含有给定的注解
				if(clazz.isAnnotationPresent(CustomComponentScan.class)){
					CustomComponentScan scan =
							(CustomComponentScan) clazz.getAnnotation(CustomComponentScan.class);
					System.out.println(scan.value());
					System.out.println(clazz.newInstance());
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
